package com.fx.zmlzml.filter;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * XSS过滤器
 * 用于拦截并清理所有HTTP请求中的潜在XSS代码
 */
public class XssFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 过滤器初始化，无需特殊处理
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // 将原始请求包装成XSS防护请求
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        XssHttpServletRequestWrapper wrappedRequest = new XssHttpServletRequestWrapper(httpRequest);
        
        // 继续过滤链
        chain.doFilter(wrappedRequest, response);
    }

    @Override
    public void destroy() {
        // 过滤器销毁，无需特殊处理
    }
}